package com.isscollage.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.PessimisticLockingFailureException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;

import com.isscollage.entity.Users;
import com.isscollage.service.LoginService;

@Controller
@RequestMapping("login")
@SessionAttributes("user")
public class LoginController   {
	
	@Autowired
	private LoginService loginService;
	
	@RequestMapping("toLogin")
	public String toLogin() {
		System.out.println("=====进入用户登录=======");
		return "login";
	}
	@RequestMapping("toRegister")
	public String toRegister() {
		return "register";
	}
	
	@RequestMapping("register")
	public String register(Users user){
		boolean flag=loginService.register(user);
		if(flag){
			return "redirect:index";
		}
		return "register";
	}
	
	@RequestMapping(value="uploadPhoto",produces="text/html;charset=utf-8")
	@ResponseBody
	public String uploadPhoto(HttpSession session,@RequestParam("cardPhoto") MultipartFile file) throws Exception{
		//获取文件名字
		String fileName=file.getOriginalFilename();
		//放到某路径下
//		String path="F:\\upload";
		//将文件放到服务器上
		String path1=session.getServletContext().getRealPath("upload");
		//拼文件，拼成完整路径
		File newFile=new File(path1,fileName);
		if(!newFile.exists()){
			//创建文件newFile
			newFile.mkdirs();
		}
		//将路径写入磁盘中
		file.transferTo(newFile);
		return fileName;
	}
	
	@RequestMapping(value="/download")
	@ResponseBody
	public ResponseEntity<byte[]> download(HttpSession session,@RequestParam String photo) throws Exception{
		System.out.println("	文件名为："+photo);
		//下载文件路径
		String realPath = session.getServletContext().getRealPath("upload");
		File file = new File(realPath + "/" + photo);
		//下载显示的文件名，解决中文名称乱码问题
		String downloadFileName = new String(photo.getBytes("UTF-8"),"iso-8859-1");
		HttpHeaders headers = new HttpHeaders();
		//通知浏览器以attachment(下载方式)打开图片
		headers.setContentDispositionFormData("attachment", downloadFileName);
		//application/octet-strseam: 二进制流数据(最常见的文件下载)
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);
	}
	
	@RequestMapping("index")
	public String index() {
		return "../../index";
	}
	
	@RequestMapping("main")
	public String main() {
		System.out.println("=====进入用户个人中心=======");
		return "main";
	}
	
	@RequestMapping(value="login")
	public String login(Model model,  String userid,  String password) {
		Users user = loginService.login(userid, password);
		System.out.println(user);
		if(user!=null) {
			model.addAttribute("user", user);
			return "main";
		}
		return "login";
	}
	
	@RequestMapping("loginOut")
	public String loginOut(SessionStatus status){
		status.setComplete();
		return "redirect:index";	
	}
	
	@RequestMapping("loginIn")
	@ResponseBody
	public boolean loginIn(Model model,String userId,String password){
		Users user=loginService.login(userId, password);
		if(user!=null){
			model.addAttribute("user",user);
			return true;
		}
		return false;
	}
	
	
}
